//
//  ZYPhoneRecoverVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/29.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYPhoneRecoverVC.h"
#import "ZYPhoneRecoverView.h"
#import "ZYRecoverPasswordViewModel.h"
#import "ZYRegisterViewModel.h"
#import "ZYLoginMainVC.h"
#import "JKCountDownButton.h"

@interface ZYPhoneRecoverVC ()<ZYPhoneRecoverViewDelegate>
@property(nonatomic,strong) ZYPhoneRecoverView *phoneRecoverView;
@property(nonatomic,strong) ZYRecoverPasswordViewModel *viewModel;
@property(nonatomic,strong) ZYRegisterViewModel *registerViewModel;
@end

@implementation ZYPhoneRecoverVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self setupTap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(_type == ZYRecoverPasswordTypeEmail){
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_EMAIL_RECOVER_CONTROLLER_TITLE];
    }else{
        self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PHONE_RECOVER_CONTROLLER_TITLE];
    }
}

-(void)setup{
    _phoneRecoverView = [[ZYPhoneRecoverView alloc] initWithFrame:self.view.frame];
    _phoneRecoverView.delegate = self;
    _phoneRecoverView.type = _type;
    [_phoneRecoverView setupTitle];
    [self.view addSubview:_phoneRecoverView];
    
    ZYWeakSelf(weakSelf)
    [_phoneRecoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(ZYSCREEN_H * 0.1);
        make.bottom.equalTo(weakSelf.view);
        make.trailing.equalTo(weakSelf.view);
        make.leading.equalTo(weakSelf.view);
    }];
}

#pragma mark - ZYPhoneRecoverViewDelegate
//手机或邮箱找回密码
-(void)phoneRecoverView:(ZYPhoneRecoverView *)phoneRecoverView doConfirm:(id)button{
    [self dismissKeyboard];//关闭键盘
    self.viewModel.phone = [phoneRecoverView getUsername];
    self.viewModel.password = [phoneRecoverView getPassword];
    self.viewModel.rePassword = [phoneRecoverView getRePassword];
    self.viewModel.code = [phoneRecoverView getCode];
    self.viewModel.type = self.type;
    @weakify(self);
    [[self.viewModel.phoneCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_RECOVER_PASSWORD_SUCCESS]];
        ZYLoginMainVC *vc = [[ZYLoginMainVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    //监听状态
    [[self.viewModel.phoneCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        ((UIButton *)button).enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
    
}
//发送验证码
-(void)phoneRecoverView:(ZYPhoneRecoverView *)phoneRecoverView doGetCode:(id)button{
    [self dismissKeyboard];//关闭键盘
    JKCountDownButton *codeBtn =(JKCountDownButton *) button;
    NSString *phone = [phoneRecoverView getUsername];
    self.registerViewModel.phone = phone;
    if(_type == ZYRecoverPasswordTypeEmail){
        self.registerViewModel.modifyType = ZYModifyTypeEmail;
    }else{
        self.registerViewModel.modifyType = ZYModifyTypePhone;
    }
    [[self.registerViewModel.codeCommand execute:nil] subscribeNext:^(id x) {
        codeBtn.enabled = NO;
        //button type要 设置成custom 否则会闪动
        [codeBtn startCountDownWithSecond:60];
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_GET_CODE_SUCCESS]];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    //监听状态
    [[self.registerViewModel.codeCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        codeBtn.enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}

#pragma mark - Getter
-(ZYRecoverPasswordViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYRecoverPasswordViewModel alloc] init];
    }
    return _viewModel;
}

-(ZYRegisterViewModel *)registerViewModel{
    if(!_registerViewModel){
        _registerViewModel = [[ZYRegisterViewModel alloc] init];
    }
    return _registerViewModel;
}

@end
